package com.tan.steamship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tan.steamship.domain.ShipsPo;
import com.tan.steamship.dto.ShipsDto;
import com.tan.steamship.mapper.ShipsMapper;
import com.tan.steamship.service.ShipsService;
import com.tan.steamship.vo.ShipsVo;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 85726
 * @description 针对表【ships(游轮信息表)】的数据库操作Service实现
 * @createDate 2025-01-20 14:51:14
 */
@Service
public class ShipsServiceImpl extends ServiceImpl<ShipsMapper, ShipsPo>
        implements ShipsService {

    @Override
    public Page<ShipsPo> listShips(String shipName, int page, int size) {
        LambdaQueryWrapper<ShipsPo> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(shipName)) {
            queryWrapper.like(ShipsPo::getShipName, shipName);
        }
        queryWrapper.orderByDesc(ShipsPo::getCreateTime);
        Page<ShipsPo> pageShip = this.page(new Page<>(page, size), queryWrapper);
        return pageShip;
    }

    @Override
    public void saveOrUpdate(ShipsDto shipsDto) {
        ShipsPo shipsPo = BeanUtil.copyProperties(shipsDto, ShipsPo.class);
        if ((ObjectUtil.isNotEmpty(shipsDto.getId()))) {
            shipsPo.setUpdateTime(new Date());
        }
        this.saveOrUpdate(shipsPo);
    }

    @Override
    public ShipsVo detail(String id) {
        ShipsPo shipsPo = this.getById(id);
        if (ObjectUtil.isNotEmpty(shipsPo)) {
            return BeanUtil.copyProperties(shipsPo, ShipsVo.class);
        }
        return null;
    }

    @Override
    public List<String> listShipName() {
        List<ShipsPo> list = this.list();
        if (ObjectUtil.isNotEmpty(list)) {
            return list.stream().map(ShipsPo::getShipName).collect(Collectors.toList());
        }
        return null;
    }
}




